package com.monco.controller;

import com.monco.common.response.ApiResult;
import com.monco.common.utils.FileUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author monco
 */
@Api(tags = "文件上传模块API")
@RestController
@RequestMapping("file")
public class FileController {

    @Value("${file.path}")
    private String filePath;

    @ApiOperation("上传文件")
    @PostMapping(value = "/uploadFile", consumes = "multipart/*", headers = "content-type=multipart/form-data")
    @ResponseBody
    public ApiResult uploadFile(@RequestParam MultipartFile file) throws Exception {
        String fileName;
        try {
            fileName = FileUtils.uploadFile(file, filePath);
        } catch (Exception e) {
            e.printStackTrace();
            return ApiResult.error("上传文件异常");
        }
        return ApiResult.ok(fileName);
    }
}
